local skel = fk.CreateSkill {
  name = "efengqi__dimeng",
}

Fk:loadTranslationTable{
  ["efengqi__dimeng"] = "缔盟",
  [":efengqi__dimeng"] = "结束阶段，或当你受到伤害后，你可以令两名手牌数之和为6的角色各摸一张牌，然后你可以重复此流程。",

  ["efengqi__dimeng_active"] = "缔盟选人",
  ["#efengqi__dimeng-choose"] = "缔盟：你可以令两名手牌数之和为6的角色各摸一张牌",
  ["@efengqi__dimeng"] = "缺%arg",

  ["$efengqi__dimeng1"] = "以和为贵，以和为贵。",
  ["$efengqi__dimeng2"] = "合纵连横，方能以弱胜强。",
}

local on_cost = function (self, event, target, player, data)
  local _, dat = player.room:askToUseActiveSkill(player, { skill_name = "efengqi__dimeng_active", prompt = "#efengqi__dimeng-choose"})
  if dat then
    player.room:sortByAction(dat.targets)
    event:setCostData(self, { tos = dat.targets })
    return true
  end
end

local on_use = function(self, event, target, player, data)
  local room = player.room
  for _, p in ipairs(event:getCostData(self).tos) do
    if not p.dead then
      p:drawCards(1, skel.name)
    end
  end
  while not player.dead do
    local _, dat = player.room:askToUseActiveSkill(player, { skill_name = "efengqi__dimeng_active", prompt = "#efengqi__dimeng-choose"})
    if not dat then break end
    local tos = dat.targets
    room:sortByAction(tos)
    room:doIndicate(player, tos)
    player:broadcastSkillInvoke(skel.name)
    for _, p in ipairs(tos) do
      if not p.dead then
        p:drawCards(1, skel.name)
      end
    end
  end
end

skel:addEffect(fk.Damaged, {
  anim_type = "masochism",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name)
  end,
  on_cost = on_cost,
  on_use = on_use,
})

skel:addEffect(fk.EventPhaseStart, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and player.phase == Player.Finish
  end,
  on_cost = on_cost,
  on_use = on_use,
})


return skel
